//
//  lxAsyncImageview.m
//  luodansc
//
//  Created by 倫 星 on 2012/11/21.
//  Copyright (c) 2012年 方正株式会社. All rights reserved.
//

#import "lxAsyncImageview.h"
#ifndef LUN_IMG_LOADING_VIEW_TAG
    #define LUN_IMG_LOADING_VIEW_TAG 1226
#endif

// This class demonstrates how the URL loading system can be used to make a UIView subclass
// that can download and display an image asynchronously so that the app doesn't block or freeze
// while the image is downloading. It works fine in a UITableView or other cases where there
// are multiple images being downloaded and displayed all at the same time. 

@implementation lxAsyncImageview
@synthesize imageView = _imageView;
@synthesize lxAlignmentCenter = _lxAlignmentCenter;

- (id)initWithFrame:(CGRect)frame image:(UIImage*)image
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        _lxAlignmentCenter = YES;
        self.frame = frame;
        _imageView = [[UIImageView alloc] initWithFrame:self.bounds];
        _no_image = image;
        _imageView.image = _no_image;
        [self setImageConfig];
        [self addSubview:_imageView];
    }
    return self;
}

- (void)setImageConfig {
    _imageView.contentMode = UIViewContentModeScaleAspectFit;
    _imageView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight );
    [_imageView setNeedsLayout];
    [self setNeedsLayout];
    
    [self setImageViewSize];
}

- (void)setLxAlignmentCenter:(BOOL)lxAlignmentCenter {
    _lxAlignmentCenter = lxAlignmentCenter;
    [self setImageViewSize];
}

- (void)setImageViewSize {
    if (YES == _lxAlignmentCenter) {
        return;
    }
    
    if (nil == self.imageView) {
        return;
    }
    
    float view_width = self.bounds.size.width;
    float view_height = self.bounds.size.height;
    
    float image_width = self.imageView.image.size.width;
    float image_height = self.imageView.image.size.height;
    
    float width = view_width;
    float height = view_height;
    
    if (image_width < view_width) {
        width = image_width;
    }
    
    if (image_height < view_height) {
        height = image_height;
    }

    CGRect ret = CGRectMake(0, 0, width, height);
    self.imageView.frame = ret;
}

- (void)dealloc {
    _lxAlignmentCenter = YES;
    [_imageView release];
    [super dealloc];
}


- (void)loadImageFromURL:(NSString*)str_url {
    if (nil == str_url) {
        return;
    }
    [self sendRequest:str_url];
}

//just in case you want to get the image directly, here it is in subviews
- (UIImage*) image {
	return _imageView.image;
}

#pragma mark -
#pragma mark ASIHTTPRequestDelegate Methods
- (ASIHTTPRequest *)sendRequest:(NSString*)str_url {
    [self showWaiting];
    NSURL *url = [NSURL URLWithString:str_url];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request setDelegate:self];
    [request startAsynchronous];
    return request;
}

- (void)getResponse:(ASIHTTPRequest *)request {
    NSData *reponsedata = [request responseData];
    
    if (nil == reponsedata) {
        NSLog(@"reponsedate is nil");
        _imageView.image = _no_image;
        return;
    }
    
    if (![reponsedata isKindOfClass:[NSData class]]) {
        NSLog(@"reponsedate is nil");
        _imageView.image = _no_image;
        return;
    }
    
    _imageView.image = [UIImage imageWithData:reponsedata];
	[self setImageConfig];

    return;
}

- (void)requestFinished:(ASIHTTPRequest *)request {
    [self getResponse:request];
    [self hideWaiting];
}

- (void)requestFailed:(ASIHTTPRequest *)request {
    _imageView.image = _no_image;
    [self setImageConfig];
    [self hideWaiting];
}

////////////////UIActivityIndicatorView add/////////////////
- (void)showWaiting {
    int width = 32;
    int height = 32;
    
    CGRect ret = self.bounds;
    int x = ret.size.width;
    int y = ret.size.height;
    
    ret = CGRectMake((x - width) / 2, (y - height) / 2, width, height);
    UIActivityIndicatorView *loadView = [[UIActivityIndicatorView alloc] initWithFrame:ret];
    
    [loadView startAnimating];
    loadView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
    
    [loadView setTag:LUN_IMG_LOADING_VIEW_TAG];
    [self addSubview:loadView];
    
    [loadView release];
}

- (void)hideWaiting {
    [[self viewWithTag:LUN_IMG_LOADING_VIEW_TAG] removeFromSuperview];
}

@end
